<script setup lang="ts">
import {CommonTable} from "@/components/MyComponent/CommonTable/tableInterface";
import {Weapon} from "@/api/sys/dnf/weapon/types";
import {addWeaponApi, deleteWeaponApi, editWeaponApi, getWeaponByIdApi, getWeaponListApi} from "@/api/sys/dnf/weapon";
import {DictType, Perm} from "@/utils/enum-type";
import {buyUserWeaponApi} from "@/api/sys/dnf/user-weapon";
import {useMoneyStore} from "@/store/views/sys/dnf/money";
import {openConfirm} from "@/components/MyComponent/XMessageBox";
import {commonFormat} from "@/utils/format-table";
import {getDictOptions} from "@/api/sys/admin/dict";

const options: CommonTable<Weapon, Weapon> = {
  api: {
    add: addWeaponApi,
    remove: deleteWeaponApi,
    edit: editWeaponApi,
    get: getWeaponByIdApi,
    getList: getWeaponListApi,
  },
  perms: {
    add: Perm.DnfWeaponAdd,
    remove: Perm.DnfWeaponDelete,
    edit: Perm.DnfWeaponEdit,
    view: Perm.DnfWeaponView,
  },
  attrs: [
    {
      prop: 'name',
      label: '装备名称',
      columnWidth: 140
    },
    {
      prop: 'imageUrl',
      label: '图片',
      formType: 'image',
      image: {
        size: 2,
        borderRadius: '0.5em',
        path: 'weapon'
      }
    },
    {
      prop: 'weaponType',
      label: '装备类型',
      formType: 'select',
      code: 'dnf_weapon_type'
    },
    {
      prop: 'level',
      label: '初始等级',
      formType: 'number',
    },
    {
      prop: 'price',
      label: '价格',
      formType: 'number',
      query: false
    }, {
      prop: 'priceType',
      label: '价格类型',
      formType: 'select',
      code: 'dnf_price_type'
    }, {
      prop: 'remark',
      label: '描述',
      formType: 'textarea',
      query: false,
      sort: false,
      columnWidth: 400,
      notDisplayOn: ['list']
    },
    {
      prop: 'status',
      label: '状态',
      formType: 'radio',
      notDisplayOn:['list-phone'],
      code: DictType.Status
    }
  ],
  title: '装备配置',
  form: {
    labelWidth: 100,
    initialForm: {id: 0, weaponType: 1, level: 0, name: '', price: 1000, priceType: 1, status: 1},
    rules: {
      name: [{required: true, message: '请输入名称', trigger: 'blur'}],
      level: [{required: true, message: '请输入名称', trigger: 'blur'},
        {type: 'number', message: '0-10之间', min: 0, max: 10}],
      weaponType: [{required: true, message: '请选择装备类型', trigger: 'blur'}],
      price: [{required: true, message: '请输入价格', trigger: 'blur'},
        {
          type: 'number', message: '你想零元购?', min: 1
        }],
      priceType: [{required: true, message: '请选择价格类型', trigger: 'blur'}],
    }
  }
}
const moneyStore = useMoneyStore()
const {getList} = moneyStore

// 购买这个武器
async function buy(id: number) {
  const weapon = await getWeaponByIdApi(id)
  const options = await getDictOptions(DictType.DnfPriceType)
  const msg = `确定购买[${weapon.name}],费用[${weapon.price}${commonFormat(weapon.priceType, options)}]`
  if (await openConfirm(msg)) {
    buyUserWeaponApi(id).then(() => {
      ElMessage.success('购买成功')
      getList()
    })
  }
}

const url = ref('')
</script>
<template>
  <dnf-user-info/>
  <x-common-table :options="options">
    <template #operation="{row}" v-perm="[Perm.DnfWeaponBuy]">
      <el-button link type="primary" size="small" @click="buy(row.id)">
        <i-ep-shopping-cart/>
        <span>购买</span>
      </el-button>
    </template>
  </x-common-table>
</template>
<style scoped lang="scss">
</style>